package com.woniuxy.service.impl;

import com.woniuxy.ResponseEntity.ResponseEntity;
import com.woniuxy.Utils.JwtUtil;
import com.woniuxy.dao.ShiroDao;
import com.woniuxy.entity.Rbacmanager;
import com.woniuxy.entity.UserDto;
import com.woniuxy.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class LoginServiceImp implements LoginService {
    @Autowired(required = false)
    ShiroDao shiroDao;
    public String findPassword(String username) {
        Rbacmanager rbacmanager =shiroDao.findPassword(username);
        return rbacmanager.getPassword();
    }

    @Override
    public ResponseEntity login(Rbacmanager rbacmanager) {
        //调用方法查询数据库
         Rbacmanager rbac=shiroDao.login(rbacmanager);
         //把密码设置成空的，目前只是偷懒，正规流程是返回一个新的dto

        //创建token,使用我们自定义的token创建工具
        String token= JwtUtil.sign(rbacmanager.getAccount(),rbacmanager.getPassword());

        //不直接返回所有用户数据，返回的是一个封装的对象
        UserDto user = new UserDto(rbac.getAccount(),token,rbac.getStatus());
        if (rbac.getId()!=null) {
            //创建返回对象
          return  new ResponseEntity(200,"登录成功",user);
        }else {
            return   new ResponseEntity(403,"登录失败",user);

        }
    }
}
